package com.yimi.appstore.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.yimi.appstore.db.ModeManagerBase;
import com.yimi.appstore.download.DownloadEntry;

public class DownloadRecordEntityManager extends ModeManagerBase{
	private static DownloadRecordEntityManager sInstance;
	
	private DownloadRecordEntityManager(){
	}

	/**
	 * 单例实体化
	 * @return
	 */
	public static DownloadRecordEntityManager getInstance(){
		if(sInstance==null)
			sInstance=new DownloadRecordEntityManager();
		return sInstance;
	}
	
	public void notifyChange(Context context){
		context.getContentResolver().notifyChange(Uri.parse("content://com.yimi.appstore.appstoreprovider"), null);
	}
	
	/**
	 * 判断下载是否存在
	 * @param url 
	 * @return
	 */
	public boolean isDownloadExists(String url){
		Cursor c=_db.query(DownloadRecordEntity.DOWNLOADS_TABLE_NAME, null,DownloadRecordEntity.URL+"=?", new String[]{url}, null, null, null);
		int count=c.getCount();
		c.close();
		if(count==0)
			return false;
		return true;
	}
	
	/**
	 * 添加下载
	 * @param downloadEntry
	 * @return
	 */
	public int addDownload(DownloadEntry downloadEntry) {
		return  (int) _db.insert(DownloadRecordEntity.DOWNLOADS_TABLE_NAME, null, getValues(downloadEntry));
    }
	
	/**
	 * 更新下载状态
	 * @param downloadEntry
	 */
	public void updateDownload(DownloadEntry downloadEntry){
		_db.update(DownloadRecordEntity.DOWNLOADS_TABLE_NAME, getValues(downloadEntry), DownloadRecordEntity._ID+"=?", new String[]{downloadEntry.getId()+""});
	}

    /**
     * 根据id查找下载
     * @param id
     * @return
     */
    public Cursor getDownload(int id) {
        final String[] selectArgs = { Integer.toString(id) };
        return _db.query(DownloadRecordEntity.DOWNLOADS_TABLE_NAME, null, DownloadRecordEntity._ID + "=?", selectArgs,
                null, null, null, "1");
    }
    
    /**
     * 获取所有下载记录
     * @return
     */
    public Cursor getAllDowloads(){
        return _db.query(DownloadRecordEntity.DOWNLOADS_TABLE_NAME, null, null, null,
                null, null, null, null);
    }

    /**
     * 删除下载记录
     * @param id
     */
    public void dropDownload(int id) {
        final String sql = "DELETE FROM " + DownloadRecordEntity.DOWNLOADS_TABLE_NAME +" WHERE " + DownloadRecordEntity._ID + " = ?;";
        final Object[] params = { id };
        _db.execSQL(sql, params);
    }

    public void close() {
        _db.close();
    }
    
    private ContentValues getValues(DownloadEntry downloadEntry){
    	ContentValues values = new ContentValues();
		values.put(DownloadRecordEntity.RESOURCE_ID, downloadEntry.getResourceId());
		values.put(DownloadRecordEntity.URL, downloadEntry.getUrl());
		values.put(DownloadRecordEntity.MEDIA_URI, downloadEntry.getMediaUri());
		values.put(DownloadRecordEntity.MIME_TYPE, downloadEntry.getMimeType());
		values.put(DownloadRecordEntity.FILE_NAME, downloadEntry.getFileName());
		values.put(DownloadRecordEntity.LOCAL_PATH, downloadEntry.getPath());
		values.put(DownloadRecordEntity.LASTMOD, downloadEntry.getLastMod());
		values.put(DownloadRecordEntity.TOTAL_BYTES, downloadEntry.getTotalBytes());
		values.put(DownloadRecordEntity.CURRENT_BYTES, downloadEntry.getCurrentBytes());
		values.put(DownloadRecordEntity.ICON_NAME, downloadEntry.getIconName());
		return values;
    }
}
